package com.ruoyi.datastatistics.service.impl;

import com.ruoyi.common.core.domain.Parameter;
import com.ruoyi.common.core.domain.Quaparameter;
import com.ruoyi.common.core.domain.entity.TPeaplegroupVO;
import com.ruoyi.common.core.domain.entity.TPeopleTypeVO;
import com.ruoyi.common.core.feign.ModulesSystemFeign;
import com.ruoyi.datastatistics.domain.*;
import com.ruoyi.datastatistics.mapper.ItemevalutionMapper;
import com.ruoyi.datastatistics.service.ItemevaluationService;
import com.ruoyi.system.api.domain.SysDept;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ItemevaluationServiceImpl implements ItemevaluationService {
    @Autowired
    private HomepageServiceImpl homepageService;
    @Autowired
    private ItemevalutionMapper itemevalutionMapper;
    @Autowired
    public ModulesSystemFeign systemFeign;
    @Override
    public ItemevalutionVO getItemevalution(Parameter parameter) {
        parameter = systemFeign.setParameter(parameter);
        ItemevalutionVO itemevalutionVO = itemevalutionMapper.getItemevalution(parameter);
        return itemevalutionVO;
    }

    @Override
    public List<TPeopleTypeVO> getPeopletypelist() {
        List<TPeopleTypeVO> peopleTypeList = itemevalutionMapper.getAllList();
        return peopleTypeList;
    }

    @Override
    public List<TPeaplegroupVO> getPeoplegroup(@RequestParam int id) {
        List<TPeaplegroupVO> peoplegroupList = itemevalutionMapper.getgroupList(id);
        return peoplegroupList;
    }

    @Override
    @Transactional
    public Map<String,Object> getItemecount(Quaparameter quaparameter) {
        // 保存子类字段的临时值
        String tempIdcard = quaparameter.getIdcard();
        String tempName = quaparameter.getName();
        int tempStart = quaparameter.getStart();
        int tempLimit = quaparameter.getLimit();
        int tempPage = quaparameter.getPage();
        String tempIspassscreening = quaparameter.getIspassscreening();
        String tempSex = quaparameter.getSex();
        String tempTowncountry = quaparameter.getTowncountry();
        Integer tempPeopleTypeId = quaparameter.getPeopleTypeId();
        Integer tempAgeGroupId = quaparameter.getAgeGroupId();

        Parameter parameter = systemFeign.setParameter(quaparameter);
        BeanUtils.copyProperties(parameter, quaparameter);
        // 恢复子类字段
        quaparameter.setIdcard(tempIdcard);
        quaparameter.setName(tempName);
        quaparameter.setStart(tempStart);
        quaparameter.setLimit(tempLimit);
        quaparameter.setPage(tempPage);
        quaparameter.setIspassscreening(tempIspassscreening);
        quaparameter.setSex(tempSex);
        quaparameter.setTowncountry(tempTowncountry);
        quaparameter.setPeopleTypeId(tempPeopleTypeId);
        quaparameter.setAgeGroupId(tempAgeGroupId);

        List<Itemecountlist> datalist = new ArrayList<>();
        List<Itemecountlist> data = new ArrayList<>();
        if (itemevalutionMapper.Dropdepts() == 0){
            if (itemevalutionMapper.Dropstats() == 0){
                SysDept sysDept = itemevalutionMapper.selectSysDept(Long.valueOf(quaparameter.getDeptid()));
                if (sysDept.getType().equals("0")){
                    if(itemevalutionMapper.createdepts(quaparameter) >= 0){
                        if(itemevalutionMapper.createstats(quaparameter) >= 0){
                            datalist = itemevalutionMapper.getItemcountlist();
                            for (Itemecountlist itemecountlist : datalist){
                                if (itemecountlist.getShengaozuidazhi() > 0){
                                    data.add(itemecountlist);
                                }
                            }
                        }
                    }
                } else if (sysDept.getType().equals("1")) {
                    if(itemevalutionMapper.createdeptspro(quaparameter) >= 0){
                        if(itemevalutionMapper.createstats(quaparameter) >= 0){
                            datalist = itemevalutionMapper.getItemcountlist();
                            for (Itemecountlist itemecountlist : datalist){
                                if (itemecountlist.getShengaozuidazhi() > 0){
                                    data.add(itemecountlist);
                                }
                            }
                        }
                    }
                }

            }
        }
        //查询国民体质测试所有指标信息
        List<ZhiBiaoXinXi> zhibiaoxinxiList = itemevalutionMapper.selectById(26);
        List<String> titleList = new ArrayList<String>(); //前端表格column的title
        List<Map<String,Object>> titleList2 = new ArrayList<Map<String,Object>>(); //前端表格第二行表头column的title
        Map<String, Object> map = new HashMap<String, Object>();
        for (ZhiBiaoXinXi zhiBiaoXinXi : zhibiaoxinxiList) {
            Map<String, Object> temp0 = new HashMap<String, Object>();
            Map<String, Object> temp1 = new HashMap<String, Object>();
            Map<String, Object> temp2 = new HashMap<String, Object>();
            Map<String, Object> temp3 = new HashMap<String, Object>();
            Map<String, Object> temp4 = new HashMap<String, Object>();
            titleList.add(zhiBiaoXinXi.getZhibiaomingcheng());
            temp0.put("field", zhiBiaoXinXi.getColumncode() + "count");
            temp0.put("title", "人数");
            temp1.put("field", zhiBiaoXinXi.getColumncode() + "zuidazhi");
            temp1.put("title", "最大值");
            temp2.put("field", zhiBiaoXinXi.getColumncode() + "zuixiaozhi");
            temp2.put("title", "最小值");
            temp3.put("field", zhiBiaoXinXi.getColumncode() + "pingjunzhi");
            temp3.put("title", "平均值");
            temp4.put("field", zhiBiaoXinXi.getColumncode() + "biaozhuncha");
            temp4.put("title", "标准差");
            titleList2.add(temp0);
            titleList2.add(temp1);
            titleList2.add(temp2);
            titleList2.add(temp3);
            titleList2.add(temp4);
        }
        map.put("data", data);
        map.put("titleList", titleList);
        map.put("titleList2", titleList2);
        return map;
    }

}
